package com.kantboot.business.shop.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.kantboot.business.shop.domain.entity.BusShopGoodsType;
import com.kantboot.business.shop.domain.i18n.BusShopGoodsTypeI18n;
import com.kantboot.business.shop.repository.BusShopGoodsTypeI18nRepository;
import com.kantboot.business.shop.service.IBusShopGoodsTypeI18nService;
import com.kantboot.business.shop.service.IBusShopGoodsTypeService;
import com.kantboot.global.data.change.annotaion.DataChange;
import com.kantboot.global.data.change.common.CommonDataChangeKey;
import com.kantboot.util.common.http.HttpRequestHeaderUtil;
import com.kantboot.util.core.redis.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusShopGoodsTypeI18nServiceImpl implements IBusShopGoodsTypeI18nService {

    @Resource
    private BusShopGoodsTypeI18nRepository repository;

    @Resource
    private IBusShopGoodsTypeService goodsTypeService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private HttpRequestHeaderUtil httpRequestHeaderUtil;

    @Override
    public List<BusShopGoodsTypeI18n> getByLanguageCode(String languageCode) {
        return repository.getByLanguageCode(languageCode);
    }

    @Override
    public List<BusShopGoodsTypeI18n> getByGoodsTypeId(Long goodsTypeId) {
        return repository.getByGoodsTypeId(goodsTypeId);
    }

    @Override
    public List<BusShopGoodsType> getAll() {
        String languageCode = httpRequestHeaderUtil.getLanguageCode();
        String goodTypeJson = redisUtil.get("goodsTypeI18n:" + languageCode);
        if (goodTypeJson != null) {
            return JSONArray.parseArray(goodTypeJson, BusShopGoodsType.class);
        }
        List<BusShopGoodsType> all = goodsTypeService.getAll();
        for (BusShopGoodsType busShopGoodsType : all) {
            BusShopGoodsTypeI18n byLanguageCodeAndGoodsTypeId = repository.getByLanguageCodeAndGoodsTypeId(languageCode, busShopGoodsType.getId());
            if (byLanguageCodeAndGoodsTypeId == null) {
                continue;
            }
            if (byLanguageCodeAndGoodsTypeId.getFileIdOfIcon()!=null){
                busShopGoodsType.setFileIdOfIcon(byLanguageCodeAndGoodsTypeId.getFileIdOfIcon());
            }
            if (byLanguageCodeAndGoodsTypeId.getName()!=null&&!byLanguageCodeAndGoodsTypeId.getName().isEmpty()){
                busShopGoodsType.setName(byLanguageCodeAndGoodsTypeId.getName());
            }
        }
        redisUtil.set("goodsTypeI18n:" + languageCode, JSONArray.toJSONString(all));
        return all;
    }

    @DataChange(key = CommonDataChangeKey.CLIENT_INIT)
    @Override
    public BusShopGoodsTypeI18n newSave(BusShopGoodsTypeI18n busShopGoodsTypeI18n) {
        busShopGoodsTypeI18n.setId(null);
        BusShopGoodsTypeI18n byLanguageCodeAndGoodsTypeId = repository.getByLanguageCodeAndGoodsTypeId(busShopGoodsTypeI18n.getLanguageCode(), busShopGoodsTypeI18n.getGoodsTypeId());
        if (byLanguageCodeAndGoodsTypeId != null) {
            busShopGoodsTypeI18n.setId(byLanguageCodeAndGoodsTypeId.getId());
        }
        BusShopGoodsTypeI18n result = repository.save(busShopGoodsTypeI18n);
        redisUtil.delete("goodsTypeI18n:" + busShopGoodsTypeI18n.getLanguageCode());
        return result;
    }

    @DataChange(key = CommonDataChangeKey.CLIENT_INIT)
    @Override
    public void clearFileIdOfIconByLanguageCodeAndGoodsTypeId(String languageCode, Long goodsTypeId) {
        repository.clearFileIdOfIconByLanguageCodeAndGoodsTypeId(languageCode, goodsTypeId);
        redisUtil.delete("goodsTypeI18n:" + languageCode);
    }
}
